﻿package plugin.storehouse.game
{
    import com.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.storehouse.*;
    import uiresource.storehouse.ui.*;
    import view.*;
    import view.appDrag.*;
    import view.mornExtends.*;
    import vo.*;

    public class StorehouseGame extends StorehouseUI implements IRoot
    {
        private var _instance:Storehouse;
        private var _cellData:Object;
        private var _clickTime:Number = 0;
        private var _curPage:int = 0;
        private var _isDown:Boolean = false;
        private var _time:int = 0;
        private var _delayTime:int = 0;

        public function StorehouseGame(param1:Storehouse)
        {
            this._instance = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            var _loc_1:* = Control.create(StorehouseControl, this);
            ModelProxy.addObserver("warehouse", _loc_1);
            AppDragMgr.RegisterModule(this);
            return;
        }// end function

        public function initEvent() : void
        {
            btn_close.clickHandler = new Handler(this.doClose);
            btn_cleanup.clickHandler = new Handler(this.doCleanup);
            btn_next.clickHandler = new Handler(this.doNext);
            btn_previous.clickHandler = new Handler(this.doPre);
            AppDragMgr.addEventListener(AppDragEvent.DRAG, this.dragHandler);
            return;
        }// end function

        private function dragHandler(event:AppDragEvent) : void
        {
            if (!(event.targetObj is FlashItem))
            {
                return;
            }
            ModelProxy.change("bag", "dragComplate", {point:event.hitPoint, data:event.itemData, key:"warehouse"});
            return;
        }// end function

        private function doPre() : void
        {
            if (list_cell.page != 0)
            {
                var _loc_1:* = list_cell;
                var _loc_2:* = list_cell.page - 1;
                _loc_1.page = _loc_2;
            }
            txt_page.text = (list_cell.page + 1) + "/" + list_cell.totalPage;
            return;
        }// end function

        private function doNext() : void
        {
            if (list_cell.page != (list_cell.totalPage - 1))
            {
                var _loc_1:* = list_cell;
                var _loc_2:* = list_cell.page + 1;
                _loc_1.page = _loc_2;
            }
            txt_page.text = (list_cell.page + 1) + "/" + list_cell.totalPage;
            return;
        }// end function

        public function removeEvent() : void
        {
            btn_close.clickHandler = null;
            return;
        }// end function

        private function doClose() : void
        {
            this._instance.close();
            return;
        }// end function

        private function doCleanup() : void
        {
            if (this._delayTime == 0)
            {
                this._delayTime = 10;
                btn_cleanup.label = this._delayTime + "";
                btn_cleanup.disabled = true;
                this._time = setInterval(this.doTime, 1000);
                PluginManager.instance.sendToServer("warehouse", CMDStorehouse.TidyItem, null);
            }
            return;
        }// end function

        private function doTime() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this._delayTime - 1;
            _loc_1._delayTime = _loc_2;
            if (this._delayTime == 0)
            {
                btn_cleanup.label = "整理";
                btn_cleanup.disabled = false;
                clearInterval(this._time);
            }
            else
            {
                btn_cleanup.label = this._delayTime + "";
                btn_cleanup.disabled = true;
            }
            return;
        }// end function

        public function updateMsg(param1:int, param2:Object) : void
        {
            switch(param1)
            {
                case CMDStorehouse.Init:
                {
                    this._cellData = param2;
                    break;
                }
                case CMDStorehouse.Storage:
                {
                    this.addItme(param2);
                    break;
                }
                case CMDStorehouse.RemoveItem:
                {
                    this.removeItem(param2);
                    break;
                }
                case CMDStorehouse.ChangeList:
                {
                    this.changeList(param2);
                    break;
                }
                case CMDStorehouse.TidyItem:
                {
                    this._cellData = param2;
                    break;
                }
                case CMDStorehouse.Expansion:
                {
                    this._cellData.maxcount = this._cellData.maxcount + int(param2);
                    break;
                }
                default:
                {
                    return;
                    break;
                }
            }
            this.refreshItem();
            return;
        }// end function

        private function changeList(param1:Object) : void
        {
            var _loc_2:String = null;
            for (_loc_2 in param1)
            {
                
                this._cellData[_loc_2] = param1[_loc_2];
            }
            return;
        }// end function

        private function refreshItem() : void
        {
            var _loc_5:Object = null;
            var _loc_6:Object = null;
            var _loc_1:Array = [];
            var _loc_2:* = ConfigDictionary.data.item;
            var _loc_3:* = this._cellData.maxcount;
            ModelProxy.change("data", "openline", _loc_3 / 8 - 11);
            var _loc_4:int = 1;
            while (_loc_4 <= 216)
            {
                
                _loc_5 = new Object();
                if (_loc_4 <= _loc_3)
                {
                    _loc_6 = this._cellData[_loc_4];
                    if (_loc_6)
                    {
                        if (_loc_6[4])
                        {
                            _loc_5 = _loc_6[4];
                            _loc_5.count = _loc_6[1] < 2 ? ("") : (_loc_6[1]);
                        }
                        else if (_loc_2[_loc_6[0]])
                        {
                            _loc_5 = ItemObject.create(_loc_2[_loc_6[0]]);
                            _loc_5.count = _loc_6[1] < 2 ? ("") : (_loc_6[1]);
                        }
                        if (_loc_5)
                        {
                            _loc_5.param = _loc_4;
                        }
                    }
                    else
                    {
                        _loc_5 = {icon:""};
                    }
                }
                else
                {
                    _loc_5 = {lock:true, tipsMsg:"点击可扩充仓库空间", icon:""};
                }
                _loc_1.push(_loc_5);
                _loc_4++;
            }
            list_cell.array = _loc_1;
            list_cell.renderHandler = new Handler(this.cellRender);
            txt_page.text = (list_cell.page + 1) + "/" + list_cell.totalPage;
            return;
        }// end function

        private function removeItem(param1:Object) : void
        {
            delete this._cellData[param1];
            return;
        }// end function

        private function cellRender(param1:Component, param2:int) : void
        {
            if (param2 < list_cell.length)
            {
                (param1 as FlashItem).flagshow = false;
                (param1 as FlashItem).data = list_cell.array[param2];
                (param1 as FlashItem).changeSuperLvXY(5, 6);
                param1.removeEventListener(MouseEvent.CLICK, this.doMouse);
                param1.addEventListener(MouseEvent.CLICK, this.doMouse);
                param1.removeEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHnalder);
                param1.removeEventListener(MouseEvent.MOUSE_UP, this.mouseUpHnalder);
                param1.removeEventListener(MouseEvent.MOUSE_MOVE, this.moveHandler);
                param1.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHnalder);
                param1.addEventListener(MouseEvent.MOUSE_UP, this.mouseUpHnalder);
                param1.addEventListener(MouseEvent.MOUSE_MOVE, this.moveHandler);
            }
            return;
        }// end function

        protected function moveHandler(event:MouseEvent) : void
        {
            var _loc_2:FlashItem = null;
            var _loc_3:Image = null;
            var _loc_4:BitmapData = null;
            var _loc_5:Bitmap = null;
            if (this._isDown)
            {
                _loc_2 = event.currentTarget as FlashItem;
                _loc_3 = _loc_2.getChildByName("icon") as Image;
                if (_loc_3 == null || _loc_3.visible == false)
                {
                    return;
                }
                _loc_4 = new BitmapData(42, 42, true, 0);
                _loc_4.draw(_loc_3);
                _loc_5 = new Bitmap(_loc_4);
                AppDragMgr.clingyItem(_loc_2, _loc_2.dataSource, _loc_5, 2, "warehouse");
                this._isDown = false;
            }
            return;
        }// end function

        protected function mouseUpHnalder(event:MouseEvent) : void
        {
            this._isDown = false;
            return;
        }// end function

        protected function mouseDownHnalder(event:MouseEvent) : void
        {
            this._isDown = true;
            return;
        }// end function

        protected function doMouse(event:MouseEvent) : void
        {
            var _loc_5:int = 0;
            var _loc_2:* = new Date().getTime();
            var _loc_3:* = _loc_2 - this._clickTime;
            this._clickTime = _loc_2;
            var _loc_4:* = event.currentTarget.dataSource;
            if (!event.currentTarget.dataSource)
            {
                return;
            }
            if (_loc_3 < 500)
            {
                _loc_5 = _loc_4.param;
                if (_loc_5 > 0)
                {
                    PluginManager.instance.sendToServer("warehouse", CMDStorehouse.Tackout, _loc_5);
                }
            }
            else if (_loc_4.lock)
            {
                DialogMgr.instance.open(AppDialog.OPEN_LOCK2, 2);
            }
            return;
        }// end function

        private function addItme(param1:Object) : void
        {
            return;
        }// end function

        public function toPage(param1:int) : void
        {
            if ((list_cell.totalPage - 1) < param1)
            {
                list_cell.page = list_cell.totalPage - 1;
            }
            else
            {
                list_cell.page = param1;
            }
            return;
        }// end function

        public function DragOver(param1:Object, param2:Point, param3:String) : void
        {
            var _loc_4:Point = null;
            if (param3 == "bag")
            {
                _loc_4 = this.globalToLocal(param2);
                if (_loc_4.x > 0 && _loc_4.y > 0 && _loc_4.x < this.width && _loc_4.y < this.height)
                {
                    this.BagToStore(param1, param2);
                }
            }
            else if (param3 == "warehouse")
            {
                _loc_4 = this.globalToLocal(param2);
                if (_loc_4.x > 0 && _loc_4.y > 0 && _loc_4.x < this.width && _loc_4.y < this.height)
                {
                    this.doSwap(param1, param2);
                }
            }
            return;
        }// end function

        private function doSwap(param1:Object, param2:Point) : void
        {
            var _loc_3:* = this.globalToLocal(param2);
            if (_loc_3.x < list_cell.x || _loc_3.y < list_cell.y || _loc_3.x > list_cell.width + list_cell.x || _loc_3.y > list_cell.height + list_cell.y)
            {
                return;
            }
            var _loc_4:* = param1.param;
            var _loc_5:* = list_cell.page * 72 + int((_loc_3.x - list_cell.x) / 48) + int((_loc_3.y - list_cell.y) / 48) * 9 + 1;
            if (_loc_4 <= 0 || _loc_5 <= 0)
            {
                return;
            }
            PluginManager.instance.sendToServer("warehouse", CMDStorehouse.Swap, {index:_loc_4, toindex:_loc_5});
            return;
        }// end function

        private function BagToStore(param1:Object, param2:Point) : void
        {
            var _loc_3:* = this.globalToLocal(param2);
            if (_loc_3.x < 19 || _loc_3.y < 55 || _loc_3.x > 441 || _loc_3.y > 390)
            {
                return;
            }
            PluginManager.instance.sendToServer("warehouse", CMDStorehouse.Storage, param1.index);
            return;
        }// end function

    }
}
